package com.ys.service.business.impl;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.entity.business.TbBaseInfo;
import com.ys.entity.finance.TcwFinancialTransactionType;
import com.ys.entity.sys.TsUserChange;
import com.ys.mapper.business.TbBaseInfoMapper;
import com.ys.util.AESUtil;
import com.ys.util.shiro.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.mapper.sys.SysMapper;
import com.ys.service.business.TbBaseInfoService;
import com.ys.vo.R;

import java.util.Map;

@Service("tbBaseInfoService")
public class TbBaseInfoServiceImpl implements TbBaseInfoService {

	@Autowired
	private SysMapper sysMapper;


	@Autowired
	private TbBaseInfoMapper tbBaseInfoMapper;
	
	@Override
	@Transactional
	public R deleteTrue(String guid) {
		if (guid == null || guid.length() == 0) {
			return R.error("ID不能为空");
		}
		
		// 删除角色-按钮
		sysMapper.deleteTrueTbBaseInfoByGuid(guid);
		
		return R.ok();
	}

	/**
	 * 新增
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public R add(Map<String, Object> params) throws Exception {
		TbBaseInfo tbBaseInfo = JSON.parseObject(JSON.toJSONString(params), TbBaseInfo.class);
		String loginUserId = ShiroUtils.getUserId();
		TsUserChange tsUserChange = new TsUserChange().setUserId(loginUserId).setStatus(1).queryFirst();
		tbBaseInfo.setStatus(1)
					.setVType("jylx")
					.setIid(0)
					.setVId(tbBaseInfo.getVName());
		String vName = params.get("vName").toString();
		String vAccountName = params.get("vAccountName").toString();
		if (vName == null || vAccountName == null) {
			return R.error("请补充必填项");
		} else if (new TbBaseInfo().setVName(vName).setVAccountName(vAccountName).count() > 0) {
			return R.error("已新增过该数据，请重新组合");
		}
		tbBaseInfoMapper.insert(tbBaseInfo);
		return R.ok(AddDto.closeAndReload());
	}

	/**
	 * 修改
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public R edit(Map<String, Object> params) throws Exception {
		TbBaseInfo tbBaseInfo = JSON.parseObject(JSON.toJSONString(params), TbBaseInfo.class);
	
		tbBaseInfoMapper.update(tbBaseInfo);
		return R.ok(AddDto.closeAndReload());
	}


}
